using SqlSugar;
using IM.Easy.Core.Services;
using IM.Easy.Core.Utils;
using IM.Easy.Infrastructure.Query;
using IM.Easy.Core.Extensions;
using IM.Easy.Entity.System;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using Microsoft.AspNetCore.Http;

namespace IM.Easy.SystemServices.Services
{
    public class FileTypesService : AppServices<FileTypes>, IFileTypesService
    {

        public FileTypesService(ISqlSugarClient db) : base(db)
        {

        }

        public FileTypes Create(FileTypes entity)
        {
            // TODO: 添加创建逻辑
            return Context.Insertable(entity).ExecuteReturnEntity();
        }

        public FileTypes GetById(Guid id)
        {
            // TODO: 添加获取逻辑
            return Context.Queryable<FileTypes>().InSingle(id);
        }

        public PageResponse<FileTypes> GetPageList(FileTypesQueryDto query)
        {
            var queryable = Context.Queryable<FileTypes>();
            var exp = Expressionable.Create<FileTypes>();
            exp = exp
;
            var page = queryable
                 .Where(exp.ToExpression())
                 .ToPageList<FileTypes, FileTypes>(query);
            return page;
        }

        public bool Update(FileTypes entity)
        {
            // TODO: 添加更新逻辑
            return Context.Updateable(entity).ExecuteCommandHasChange();
        }

        public bool Delete(params Guid[] Id)
        {
            // TODO: 添加删除逻辑
            return Context.Deleteable<FileTypes>(Id).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 识别文件类型
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public FileTypes Recognize(IFormFile file)
        {
            var fileName = file.FileName;
            var fileType = GetFirst(s => s.MimeType == file.ContentType);
            if (fileType == null) ExceptionUtils.ThrowBusinessException("无法识别的系统文件");
            return fileType;
        }
    }
}
